package com.penghk.hangzhouhouseprice.handler;

import com.penghk.hangzhouhouseprice.entity.AreaPrice;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 获取新房房价信息
 */
@Component
public class GanJiNewHousePriceHandler {
    private Logger logger = LoggerFactory.getLogger(GanJiNewHousePriceHandler.class);

    private List<Integer> prices = new ArrayList<>();

    public static final String xihuUrl = "http://hz.ganji.com/fang12/xihu/";
    public static final String gongshu = "http://hz.ganji.com/fang12/gongshu/";
    public static final String shangcheng = "http://hz.ganji.com/fang12/shangcheng/";
    public static final String xiacheng = "http://hz.ganji.com/fang12/xiacheng/";
    public static final String jianggan = "http://hz.ganji.com/fang12/jianggan/";
    public static final String binjiang = "http://hz.ganji.com/fang12/binjiang/";
    public static final String xiaoshan = "http://hz.ganji.com/fang12/xiaoshan/";
    public static final String yuhang = "http://hz.ganji.com/fang12/yuhang/";
    public static final String linan = "http://hz.ganji.com/fang12/linan/";
    public static final String fuyang = "http://hz.ganji.com/fang12/fuyang/";
    public static final String tonglu = "http://hz.ganji.com/fang12/tonglu/";
    public static final String jiande = "http://hz.ganji.com/fang12/jiande/";
    public static final String chunan = "http://hz.ganji.com/fang12/chunan/";
    public static final String hangzhouzhoubian = "http://hz.ganji.com/fang12/hangzhouzhoubian/";


    /**
     * 获取西湖区房价
     * @return
     * @throws IOException
     */
    public AreaPrice getXiHuPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("西湖区");
        getPrice(xihuUrl , areaPrice);
        return areaPrice;
    }

    /**
     * 获取拱墅区房价
     * @return
     */
    public AreaPrice getGongShuPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("拱墅区");
        getPrice(gongshu , areaPrice);
        return areaPrice;
    }

    /**
     * 获取上城区房价
     * @return
     */
    public AreaPrice getShangchengPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("上城区");
        getPrice(shangcheng , areaPrice);
        return areaPrice;
    }

    /**
     * 获取下城区房价
     * @return
     */
    public AreaPrice getXiachengPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("下城区");
        getPrice(xiacheng , areaPrice);
        return areaPrice;
    }

    /**
     * 获取江干区房价
     * @return
     */
    public AreaPrice getJiangganPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("江干区");
        getPrice(jianggan , areaPrice);
        return areaPrice;
    }

    /**
     * 获取滨江区房价
     * @return
     */
    public AreaPrice getBinjiangPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("滨江区");
        getPrice(binjiang , areaPrice);
        return areaPrice;
    }

    /**
     * 获取萧山区房价
     * @return
     */
    public AreaPrice getXiaoshanPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("萧山区");
        getPrice(xiaoshan , areaPrice);
        return areaPrice;
    }


    /**
     * 获取余杭区房价
     * @return
     */
    public AreaPrice getYuhangPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("余杭区");
        getPrice(yuhang , areaPrice);
        return areaPrice;
    }

    /**
     * 获取临安区房价
     * @return
     */
    public AreaPrice getLinanPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("临安区");
        getPrice(linan , areaPrice);
        return areaPrice;
    }

    /**
     * 获取富阳区房价
     * @return
     */
    public AreaPrice getFuyangPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("富阳区");
        getPrice(fuyang , areaPrice);
        return areaPrice;
    }

    /**
     * 获取桐庐区房价
     * @return
     */
    public AreaPrice getTongluPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("桐庐区");
        getPrice(tonglu , areaPrice);
        return areaPrice;
    }

    /**
     * 获取建德区房价
     * @return
     */
    public AreaPrice getJiandePrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("建德区");
        getPrice(jiande , areaPrice);
        return areaPrice;
    }

    /**
     * 获取淳安区房价
     * @return
     */
    public AreaPrice getChunanPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("淳安区");
        getPrice(chunan , areaPrice);
        return areaPrice;
    }

    /**
     * 获取杭州周边房价
     * @return
     */
    public AreaPrice getHangzhouzhoubianPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("杭州周边");
        getPrice(hangzhouzhoubian , areaPrice);
        return areaPrice;
    }




    private String getPrice(String url , AreaPrice areaPrice) {
        BigDecimal priceSum = new BigDecimal(0);

        try {
            Document doc = Jsoup.connect(url).get();
            Elements numEle = doc.select("span.num");
            String num = numEle.get(0).text();
            num = num.substring(0 , num.length()-1);
            int count = Integer.parseInt(num);
            logger.info("count : "+count);
            Elements head = doc.getElementsByClass("time");
            int perPage = head.size();
            int pages = count / perPage;
            int offset = count % perPage;
            if (offset > 0) {
                pages++;
            }

            for (int i = 1 ; i <= pages ;i++) {
                String conurl = url+"o"+i+"/";
                logger.info("page ："+i+"  url : "+conurl);
                Document nextDoc = Jsoup.connect(conurl).get();
                Elements priceEle = nextDoc.getElementsByClass("time");
                for (Element element : priceEle) {
                    logger.info(element.text());
                    String priceStr = element.text();
                    int endIndex = priceStr.indexOf("元");
                    if (endIndex < 0) {
                        continue;
                    }
                    priceStr = priceStr.substring(0 , endIndex);
                    int sinPrice = Integer.parseInt(priceStr);
                    if(sinPrice > 200000 || sinPrice < 6000) {
                        continue;
                    }
                    prices.add(sinPrice);
                    priceSum = priceSum.add(new BigDecimal(sinPrice));
//                    priceSum += sinPrice;
                }
                Thread.sleep(100);
            }
            BigDecimal prePri = priceSum.divide(new BigDecimal(prices.size()));
            int prePrice = prePri.intValue();
//            int prePrice = (int) (priceSum /prices.size());
            logger.info("size : "+prices.size());
            Collections.sort(prices);
            logger.info(" 最低价 ： "+prices.get(0)+" 均价 : "+prePrice+" 最高价 ："+prices.get(prices.size()-1));
            areaPrice.setLowPrice(String.valueOf(prices.get(0)));
            areaPrice.setMidPrice(String.valueOf(prePrice));
            areaPrice.setMaxPrice(String.valueOf(prices.get(prices.size()-1)));
            areaPrice.setNum(prices.size());

            prices.clear();
            return Integer.toString(prePrice);
        }catch (Exception e) {
            return "";
        }
    }
}
